﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;

namespace BAL
{
    public class UserExtend : BaseBAL<UserExtendInfo, Models.UserExtend>
    {

        /// <summary>
        /// 根据userid取得UserExtendInfo
        /// </summary>
        /// <param name="userid">userid</param>
        /// <returns></returns>
        public static UserExtendInfo GetInfoByUserId(int userid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.GetTable<Models.UserExtend>().Single(t => t.UserId == userid));
            }
        }

        /// <summary>
        /// 判断身份证号是否已存在
        /// </summary>
        /// <param name="idcard">身份证号码</param>
        /// <returns></returns>
        public static bool CheckUserExistIdCard(string idcard)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.UserExtends.Where(w => w.IdCard == idcard).Count() > 0;
            }
        }
        /// <summary>
        /// 通过身份证号查询对象
        /// </summary>
        /// <param name="idcard"></param>
        /// <returns></returns>
        public static UserExtendInfo GetInfoByIdCard(string idcard) 
        {
            using (var db =DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.GetTable<Models.UserExtend>().Single(t => t.IdCard == idcard));
            }
        }


        /// <summary>
        /// 判断联系电话是否存在
        /// </summary>
        /// <param name="idcard">联系电话</param>
        /// <returns></returns>
        public static bool CheckUserExistByTel(string tel)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.UserExtends.Where(w => w.Tel == tel).Count() > 0;
            }
        }

        /// <summary>
        /// 刷新学校名称缓存
        /// </summary>
        /// <param name="schoolid">学校id</param>
        /// <param name="newschoolname">新学校名称</param>
        public static void SetSchoolName(int schoolid, string newschoolname)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                db.GetTable<Models.UserExtend>();
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static UserExtendInfo Save(UserExtendInfo InstInfo)
        {
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
                return InstInfo;
            }
            else
            {
                return Add(InstInfo);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Delete(id);
        }

        /// <summary>
        /// 根据多个Userid删除信息
        /// </summary>
        /// <param name="Userid">Userid列表</param>
        public static void DelByUserId(IList<int> Userid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<int> id = db.GetTable<Models.UserExtend>().Where(b => GetArray<int>.Get(Userid).Contains(b.UserId)).Select(b => b.Id).ToList();
                Delete(id);
            }
        }

        /// <summary>
        /// 根据Userid删除信息
        /// </summary>
        /// <param name="userid">Userid</param>
        public static void DelByUserId(int userid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                int id = GetInfoByUserId(userid).Id;
                DelInfo(id);
            }
        }



        /// <summary>
        /// 根据SchoolId获取用户Id列表
        /// </summary>
        /// <param name="SchoolId">SchoolId</param>
        /// <returns></returns>
        public static IList<int> GetUserIsBySchoolId(int SchoolId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.UserExtends.Where(t => t.SchoolId == SchoolId).Select(t => t.UserId).ToList();
            }
        }

        
    }
}
